class InstanceFactory:    
    _instance = None        
    ma = None
    indexer = None
    preprocessor = None
    dimReducer = None 
    fileGenerator = None
    xmlConfigurator = None  
    sectioners = None  
        
    @staticmethod          
    def getInstance():
        if not InstanceFactory._instance:
            InstanceFactory._instance = InstanceFactory() 
        return InstanceFactory._instance
    
    def setXmlConfigurator(self, configFile):
        self.configFile = configFile
        return
    def getXmlConfigurator(self):
        if self.xmlConfigurator == None:
            import xmlConfigure
            self.xmlConfigurator = xmlConfigure.Configurator(self.configFile)
        return self.xmlConfigurator

    def getMysqlAgent(self, sqlConfigDict):
        if self.ma == None:
            import mysqlAgent
            self.ma = mysqlAgent.MysqlAgent(sqlConfigDict)
        return self.ma
        
    def getIndexer(self, indexMethod):
        if self.indexer == None:
            import index
            self.indexer = index.Indexer(indexMethod)
        return self.indexer
    def getNewIndexer(self, indexMethod):
        import index
        return index.Indexer(indexMethod)
        
    def getPreprocessor(self):
        if self.preprocessor == None:
            import preprocess
            tokenizerName = self.xmlConfigurator.getTokenizerName()
            stemmerName = self.xmlConfigurator.getStemmerName()
            additionalStopwordFile = self.xmlConfigurator.getAdditionalStopwordsFilename()
            stopwordsLanguage = self.xmlConfigurator.getStopwordsLanguage()
            self.preprocessor = preprocess.Preprocessor(tokenizerName, stemmerName, additionalStopwordFile, stopwordsLanguage, self)
        return self.preprocessor

    def getSectioners(self, sectionKeys):
        if self.sectioners == None:
            self.sectioners = []
            import textSection
            for key in sectionKeys:
                self.sectioners.append(textSection.Sectioner(key))
        return self.sectioners
    
    def getDimReducer(self, reduceType, reduceMethod):
        if self.dimReducer == None:
            import reduceDim
            self.dimReducer = reduceDim.DimReducer(reduceType, reduceMethod) 
        return self.dimReducer
    
    def getFileGenerator(self, fileType):
        if self.fileGenerator == None:
            import generateFile
            self.fileGenerator = generateFile.FileGenerator(fileType)
        return self.fileGenerator

if __name__ == "__main__":
    # singleton test
    inF1 = InstanceFactory.getInstance()
    inF2 = InstanceFactory.getInstance()
    print id(inF1)
    print id(inF2)
    p1 = inF1.getPreprocessor()
    p2 = inF2.getPreprocessor()
    print id(p1)
    print id(p2)
    
    